package com.hwhp.service.console.impl;
import java.util.ArrayList;
import java.util.List;

import com.hwhp.dao.HibernateBaseDao;
import com.hwhp.entity.Role;
import com.hwhp.entity.RoleUser;
import com.hwhp.service.console.RoleService;


/**
 * 
 * @author zoycycy
 *
 */
public class RoleServiceImpl implements RoleService{
	
	private HibernateBaseDao baseDao = null ;
	
	/**
	 * 
	 * @param roleId
	 * @return
	 */
	public  Role getRoleById(String roleId){
		Role role = null ;
		
		role = (Role)baseDao.get(Role.class, roleId);
		return role ;
	}
	/**
	 * 
	 * @param rolename
	 * @return
	 */
	public  List<Role> getRoleList(String rolename){
		List<Role> roleList=new ArrayList();
		String hql="from Role as r";
		if(rolename!=null&&!"".equals(rolename)){
			hql=hql+" where r.rolename like '%"+rolename+"%'";
		}
		//roleList=HibernateUtil.query(hql);
		roleList= baseDao.query(hql);
		return roleList;
	}
	/**
	 * 
	 * @param roleidSet
	 * @return
	 */
	public  List<Role> getRoleListByIdSet(String roleidSet){
		List<Role> roleList=new ArrayList();
		String hql="from Role as r";
		if(roleidSet!=null&&!"".equals(roleidSet)){
			hql=hql+" where r.id in ("+roleidSet+")";
			//roleList=HibernateUtil.query(hql);
			roleList = baseDao.query(hql);
		}
		return roleList;
	}
	
	public  List<Role> getUnRoleListByIdSet(String roleidSet){
		List<Role> roleList=new ArrayList();
		String hql="from Role as r";
		if(roleidSet!=null&&!"".equals(roleidSet)){
			hql=hql+" where r.id not in ("+roleidSet+")";
		}
		//roleList=HibernateUtil.query(hql);
		roleList=baseDao.query(hql);
		return roleList;
	}
	
	/**
	 * 新增角色
	 * @param r
	 * @return
	 */
	public  boolean insertRole(Role r){
		boolean success = false ;
		try{
    		//HibernateUtil.save(r);
			baseDao.save(r);
    		success = true ;
    	}catch(Exception e){
    		success = false ;
    	}
		return success ;
	}
	/**
	 * 修改人员
	 * @param u
	 * @return
	 */
	public  boolean editRole(Role r){
		boolean success = false ;
		//success = HibernateUtil.update(r);
		try{
			
			baseDao.update(r);
			success = true ;
		}catch(Exception e ){
			success = false ;
			e.printStackTrace();
		}
		return success ;
	}
	/**
	 * 删除人员
	 * @param u
	 * @return
	 */
	public  boolean deleteRole(Role r){
		boolean success = false ;
		try{
			success = baseDao.delete(r);
		}catch(Exception e){
			e.printStackTrace();
		}
		//success = HibernateUtil.delete(r);
		return success ;
		
	}
	/**
	 * 
	 * @param roleUser
	 * @return
	 */
	public  boolean saveRoleUser(RoleUser roleUser){
		boolean success = false ;
		try{
    		//HibernateUtil.save(roleUser);
			baseDao.save(roleUser);
    		success = true ;
    	}catch(Exception e){
    		success = false ;
    	}
		return success ;
		
	}
	/**
	 * 
	 * @param userid
	 * @param roleid
	 * @return
	 */
	public  List<RoleUser> getRoleUserList(String userid,String roleid){
		 List<RoleUser> list=new ArrayList();
		 String hql="from RoleUser as r where 1=1 ";
		 if(userid!=null&&!"".equals(userid)){
			 hql=hql+"and r.userid='"+userid+"' ";
		 }
		 if(roleid!=null&&!"".equals(roleid)){
			 hql=hql+"and r.roleid='"+roleid+"' ";
		 }
		 return list;
	}
	
	public void setBaseDao(HibernateBaseDao baseDao) {
		this.baseDao = baseDao;
	}
	public HibernateBaseDao getBaseDao() {
		return baseDao;
	}
	
}
